$(document).ready(function() {
	//全选
	$(function() {
        $("#checkAll").click(function() {
             $('input[name="subBox"]').attr("checked",this.checked); 
         });
         var $subBox=$("input[name='subBox']");
         $subBox.click(function(){
             $("#checkAll").attr("checked",$subBox.length == $("input[name='subBox']:checked").length ? true : false);
         });
	});
	
       //多选删除
         $("#sureDeleteButton").click(function(){
        	 $('#deleteBooksPopup').slideUp("fast");
         var bookIdList="";
         $('input[name="subBox"]:checked').each(function(){
        	 bookIdList +=$(this).val()+",";
     		});
         $.ajax({
     		url:"deleteBookMulti.action?bookIdList=" + bookIdList,
     		success:function(data){
     			window.location.href="fusionSearchBook.action?number=1&bookName=&bookAuthor=&bookPress=&bookState=2";
     		}
         });
         });
         
     //  popup-add
         $("#addBook").click(function(){

         			$('#addBookPopup').slideDown("fast");
         			
         			});
         	
         	$('#closePopup').click(function(){
         		
         		$('#addBookPopup').slideUp("fast");
         	
         	});
         	
         	//未选
         	$("#deleteButton").click(function(){
         	var checkedCount=$('input[name="subBox"]:checked').length;
         	if(checkedCount==0){
         		$('#NoChecked').slideDown("fast");
         	}
         	
         	else{
         		$('#deleteBooksPopup').slideDown("fast");
         	}
         			});
         	
         	//单个图书删除
         	$(".deleteBookSingle").click(function(){
         		var bookId=$(this).children(".bookIdCurrent").val();
         		$('#deleteSingleBook').slideDown("fast");
         		$("#sureSingleBook").click(function(){
         			window.location.href="deleteBook.action?bookId="+bookId;
         		});
         		$("#cancelSingleBook").click(function(){
         			$('#deleteSingleBook').slideUp("fast");
         		});
         		
         	});
         	
         	//没选弹窗确定
         	$('#noChosedClose').click(function(){
         		$('#NoChecked').slideUp("fast");
         	});	
         	
         	//弹窗关闭
         	$('#cancel').click(function(){

         		$('#deleteBooksPopup').slideUp("fast");
         	
         		$('body').css('overflow','auto');
         	
         	});
         	
         //popup-edit
         	$(".editBookButton").click(function(){
         		var bookId=$(this).children(".bookIdCurrent").val();
         		$('#editBookPopup').slideDown("fast");
         		$.ajax({
         			url:"showEditBook.action?bookId="+bookId,
         			success:function(data){
         				var dataObject=JSON.parse(data);
         				var editBookList=dataObject["editBookList"];
         				var bookPublishdateLast=dataObject["bookPublishdateLast"];
         				var con = "";
         				con += "<img src='" + editBookList.bookImage + "'/>";
         				debugger;
         				$("#editBookName").val(editBookList.bookName);
         				$("#editBookImageNull").val(editBookList.bookImage);
         				$("#editBookAuthor").val(editBookList.bookAuthor);
         				$("#editBookPress").val(editBookList.bookPress);
         				$("#editBookPublishdate").val(bookPublishdateLast);
         				$("#editBookIsbn").val(editBookList.bookIsbn);
         				$("#showEdit").empty();
    					$("#showEdit").append(con);
         				}
         		});
         			
         	});
         	$('#closeEditPopup').click(function(){
         		$('#editBookPopup').slideUp("fast");
         	});
         	
         	
         	document.getElementById('addBookImage').addEventListener('change',function(e){  
    	        var files = this.files;  
    	        var img = new Image();  
    	        var reader = new FileReader();  
    	        reader.readAsDataURL(files[0]);  
    	        reader.onload = function(e){  
    	            var mb = (e.total/1024)/1024;  
    	            if(mb>= 2){  
    	                alert('文件大小大于2M');  
    	                return;  
    	            }  
    	            img.src = this.result;  
    	            img.style.width = "100%";  
    	            img.style.height= "100%";  
    	            document.getElementById('showAdd').style.width="200px";  
    	            document.getElementById('showAdd').style.height="240px";  
    	            document.getElementById('showAdd').innerHTML = '';  
    	            document.getElementById('showAdd').appendChild(img);  
    	        }  
    	    });
            
            document.getElementById('editBookImage').addEventListener('change',function(e){  
    	        var files = this.files;  
    	        var img = new Image();  
    	        var reader = new FileReader();  
    	        reader.readAsDataURL(files[0]);  
    	        reader.onload = function(e){  
    	            var mb = (e.total/1024)/1024;  
    	            if(mb>= 2){  
    	                alert('文件大小大于2M');  
    	                return;  
    	            }  
    	            img.src = this.result;  
    	            img.style.width = "100%";  
    	            img.style.height= "100%";  
    	            document.getElementById('showEdit').style.width="200px";  
    	            document.getElementById('showEdit').style.height="240px";  
    	            document.getElementById('showEdit').innerHTML = '';  
    	            document.getElementById('showEdit').appendChild(img);  
    	        }  
    	    });
            
          //popup-borrow
         	$(".showBorrow").click(function(){
         		var bookId=$(this).children(".borrowBookIdCurrent").val();
         		$('#borrowBookPopup').slideDown("fast");
         		$.ajax({
    				url: "showBorrowByBook.action?borrowBookid=" + bookId,
    				success:function(data){
    					var dataObject = JSON.parse(data);
    					var con = "";
    					$(dataObject).each (function(index, value) {
    						con += "<h1 class='modal-title'>"+ "The book is :" + dataObject[0].bookName + "</h1>" + "<br>" + "<br>" + "<br>" +
    						 	   "<h3 class='modal-title'>"+ "borrowed days :" + dataObject[0].mustReturn+ "days" + "</h3>" + "<br>" + "<br>" +
    						 	   "<h3 class='modal-title'>"+ "left days ：" + dataObject[0].leftDay+ "days" + "</h3>" ;
    						 
    					});
    					$(".showBorrowContent").empty();
    					$(".showBorrowContent").append(con);
    				}
    			});
         	});
         	$('#closeBorrowPopup').click(function(){
         		$('#borrowBookPopup').slideUp("fast");
         	});
         
	/*var searchBookByStatus = $("#bookState").val();
	var searchBookBySearchType = $("#searchBookType").val();
	//搜索功能保持之前的状态
	switch (searchBookByStatus) {
	case '1':
		$("#bookState1").attr("selected", "selected");
		break;
	case '0':
		$("#bookState0").attr("selected", "selected");
		break;
	}
	//搜索功能保持之前搜索类型
	switch (searchBookBySearchType) {
	case '1':
		$("#searchBookType1").attr("selected", "selected");
		break;
	case '2':
		$("#searchBookType2").attr("selected", "selected");
		break;
	case '3':
		$("#searchBookType3").attr("selected", "selected");
		break;
	case '4':
		$("#searchBookType4").attr("selected", "selected");
		break;
	case '0':
		$("#searchBookType0").attr("selected", "selected");
		break;
	}

	 $("#searchBookTypeSelection").click(function(){  
	setInterval(changDisplay, 200);

	//根据搜索类型显示相应的输入框
	function changDisplay() {
		var select = $("#searchBookTypeSelection").val();

		switch (select) {
		case '0':
			$("#searchWord").css("display", "inline");
			$("#bookStateList").css("display", "none");
			break;
		case '1':
			$("#searchWord").css("display", "inline");
			$("#bookStateList").css("display", "none");
			break;
		case '2':
			$("#searchWord").css("display", "inline");
			$("#bookStateList").css("display", "none");
			break;
		case '3':
			$("#searchWord").css("display", "inline");
			$("#bookStateList").css("display", "none");
			break;
		case '4':
			$("#searchWord").css("display", "none");
			$("#bookStateList").css("display", "inline");
			break;
		default:
			alert("请输入正确类型");
		}
	}*/
	
	 var bookStatus=$("#bookStatusHidden").val();
	 //搜索结果保持之前的状态选择
	 switch (bookStatus)
	 {
	 case '1':
		 $("#status1").attr("selected","selected");
		 break;
	 case '2':
		 $("#status2").attr("selected","selected");
		 break;
	 case '0':
		 $("#status0").attr("selected","selected");
		 break;
	 } 
	 
});